﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OlegGutsalo
{
    public class StringCalculator
    {
        public static int Add(string numbers)
        {
            if (numbers.Length == 0)
                return 0;

            //Delimiter(s) declaration
            string delimiter = ",";
            List<string> allDelimiters = new List<string>();
            //allDelimiters.Add(",");
            if (numbers.StartsWith("//") && numbers.IndexOf('\n') != -1)
            {
                delimiter = numbers.Substring(2, numbers.IndexOf('\n') - 2);
                if (delimiter.StartsWith("[") && delimiter.EndsWith("]"))
                {
                    allDelimiters = delimiter.Split("][".ToCharArray()).ToList();
                    allDelimiters.RemoveAll(str => string.IsNullOrEmpty(str));
                }
                else
                    if (delimiter.Length == 1)
                        allDelimiters.Add(delimiter);
                    else
                        throw new System.ArgumentException("Large delimiters allows only in brackers.");

                numbers = numbers.Substring(numbers.IndexOf('\n')+1);
            }

            //Spliting string into substrings
            foreach (string d in allDelimiters)
                numbers = numbers.Replace(d, ",");
            numbers = numbers.Replace('\n', ',');
            string[] allnumbs = numbers.Split(',');

            //Calculating result and looking for negative values
            List<int> negatives = new List<int>();
            int result = 0;
            foreach (string n in allnumbs)
            {
                if (int.Parse(n) < 0)
                    negatives.Add(int.Parse(n));
                if (int.Parse(n)<1000)
                    result += int.Parse(n);
            }

            //Exception Handling
            if (negatives.Count>0){
                string NegativeValues="";
                foreach (int n in negatives)
                    NegativeValues += n + ",";
                throw new System.ArgumentException(NegativeValues);
            }
            return result;
        }

        public static void Main(string[] args)
        {
        }
    }
}
